package ar.fiuba.tp7552.buildadmin.webactions.planes;

import java.math.BigDecimal;
import java.util.Date;

import com.opensymphony.xwork2.Preparable;

import ar.fiuba.tp7552.buildadmin.dao.CobroPlanDao;
import ar.fiuba.tp7552.buildadmin.model.BusinessException;
import ar.fiuba.tp7552.buildadmin.model.planes.CobroPlan;
import ar.fiuba.tp7552.buildadmin.model.planes.Plan;
import ar.fiuba.tp7552.buildadmin.service.PlanService;
import ar.fiuba.tp7552.buildadmin.webactions.BaseAction;
import ar.fiuba.tp7552.buildadmin.webactions.expensas.DetalleCobro;

public class CobroPlanAction extends BaseAction implements Preparable {

	private PlanService planService;
	private CobroPlanDao cobroplanDao;
	private Plan plan;
	private DetalleCobro detalle;
	private Date fechaCobro;
	private BigDecimal monto;
	private String comprobante;
	
	private CobroPlan cobro;
	// ~~ Constructor ====================================================
	public CobroPlanAction(PlanService planService, CobroPlanDao cobroPlanDao) {
		super();
		this.planService = planService;
		this.cobroplanDao = cobroPlanDao;
	}

	// ~~ Actions ========================================================

	@Override
	public void prepare() throws Exception {
		if (plan != null && plan.getId() != null) {
			plan = planService.get(plan.getId());
			
			if (fechaCobro != null) {
				try {
					detalle = planService.detalleCobro(plan, fechaCobro);
				} catch (BusinessException e) { }
			}
		}
		if (cobro != null && cobro.getId() != null) {
			cobro = cobroplanDao.get(cobro.getId());
		}

		
	}
	
	public String load() {
		return BaseAction.SUCCESS;
	}
	
	public String detalle() {
		try {
			detalle = planService.detalleCobro(plan, fechaCobro);
		} catch (BusinessException e) {
			this.addActionError(e.getMessage());
			return BaseAction.INPUT;
		}
		return BaseAction.SUCCESS;
	}
	
	public String registrar() {
		try {
			planService.registrarCobro(plan, fechaCobro, monto, comprobante);
		} catch (BusinessException e) {
			this.addActionError(e.getMessage());
			return BaseAction.INPUT;
		}
		return BaseAction.SUCCESS;
	}
	
	public String borrar() {
		planService.revertirCobro(cobro);
		return BaseAction.SUCCESS;
	}
	
	public String consolidar() {
		planService.consolidar(cobro, comprobante);
		return BaseAction.SUCCESS;
	}

	// ~~ getters y setters ========================================================
	public Plan getPlan() {
		return plan;
	}
	public void setPlan(Plan plan) {
		this.plan = plan;
	}
	public DetalleCobro getDetalle() {
		return detalle;
	}
	public void setDetalle(DetalleCobro detalle) {
		this.detalle = detalle;
	}
	public Date getFechaCobro() {
		return fechaCobro;
	}
	public void setFechaCobro(Date fechaCobro) {
		this.fechaCobro = fechaCobro;
	}
	public BigDecimal getMonto() {
		return monto;
	}
	public void setMonto(BigDecimal monto) {
		this.monto = monto;
	}
	public String getComprobante() {
		return comprobante;
	}
	public void setComprobante(String comprobante) {
		this.comprobante = comprobante;
	}
	public CobroPlan getCobro() {
		return cobro;
	}
	public void setCobro(CobroPlan cobro) {
		this.cobro = cobro;
	}
}
